<template>
  <el-drawer :model-value="show" title="商品选择" @close="handleCancelClick" :size="size" :close-on-click-modal="false" append-to-body>
        <el-form :inline="true" label-width="68px">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="商品名称">
                <el-input v-model="query.goodsName" clearable placeholder="商品名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="商品编号">
                <el-input class="w200" v-model="query.goodsNo" clearable placeholder="商品编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="规格名称">
                <el-input class="w200" v-model="query.skuName" clearable placeholder="规格名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="规格编号">
                <el-input class="w200" v-model="query.barcode" clearable placeholder="规格编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button type="primary" @click="loadAll">查询</el-button>
            </el-col>
          </el-row>
        </el-form>
            <el-table :data="list" @selection-change="handleSelectionChange" border :row-key="getRowKey" empty-text="暂无商品" v-loading="loading" ref="skuSelectFormRef" cell-class-name="my-cell">
              <el-table-column type="selection" width="55" :reserve-selection="true" v-if="!singleSelect" :selectable="judgeSelectable"/>
              <el-table-column label="商品信息" prop="itemId">
                <template #default="{ row }">
                  <div>{{ row.goods.goodsName }}</div>
                  <div v-if="row.goods.goodsNo">编号：{{ row.goods.goodsNo }}</div>
                  <div v-if="row.goods.bandId">品牌：{{ useBasicStore().brandMap.get(row.goods.bandId).brandName }}</div>
                </template>
              </el-table-column>
              <el-table-column label="规格信息">
                <template #default="{ row }">
                  <div>{{ row.sku.skuName }}</div>
                  <div v-if="row.sku.skuCode">编号：{{ row.sku.skuCode }}</div>
                  <div v-if="row.sku.barcode">条码：{{ row.sku.barcode }}</div>
                </template>
              </el-table-column>
              <el-table-column label="价格(元)" width="160" align="left">
                <template #default="{ row }">
                  <div v-if="row.sku.costPrice" class="flex-space-between">
                    <span>成本价：</span>
                    <div>{{ (row.sku.costPrice || row.sku.costPrice === 0) ? row.sku.costPrice : '' }}</div>
                  </div>
                  <div v-if="row.sku.sellingPrice" class="flex-space-between">
                    <span>销售价：</span>
                    <div>{{ (row.sku.sellingPrice || row.sku.sellingPrice === 0) ? row.sku.sellingPrice : '' }}</div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="重量(kg)" width="160" align="left">
                <template #default="{ row }">
                  <div v-if="row.sku.netWeight" class="flex-space-between">
                    <span>净重：</span>
                    <div>{{ (row.sku.netWeight || row.sku.netWeight === 0) ? row.sku.netWeight : '' }}</div>
                  </div>
                  <div v-if="row.sku.grossWeight" class="flex-space-between">
                    <span>毛重：</span>
                    <div>{{ (row.sku.grossWeight || row.sku.grossWeight === 0) ? row.sku.grossWeight : '' }}</div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="长宽高(cm)" align="right" width="250">
                <template #default="{ row }">
                  <div>{{ getVolumeText(row) }}</div>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="100" v-if="singleSelect" align="right">
                <template #default="{ row }">
                  <el-button link type="primary" @click="handleChooseSku(row)">选择</el-button>
                </template>
              </el-table-column>
            </el-table>
    <el-row>
      <pagination v-show="total > 0" :total="total" :page-sizes="[5, 10, 20, 50]" v-model:limit="pageReq.size" v-model:page="pageReq.page"
                  @pagination="getPage()" class="mr10"/>
    </el-row>
    <template v-slot:footer>
      <div style="width: 100%;display: flex;justify-content: space-between">
        <span>
          <el-button @click="goCreateItem" icon="Plus">创建商品</el-button>
          <el-button @click="loadAll" icon="Refresh">刷新</el-button>
        </span>
        <span>
          <el-button @click="handleCancelClick">取消</el-button>
          <el-button type="primary" @click="handleOkClick" v-if="!singleSelect">确认</el-button>
      </span>
      </div>
    </template>
  </el-drawer>
</template>
<script setup lang="ts" name="SkuSelect">
import {computed, getCurrentInstance, onMounted, reactive, ref} from 'vue';
import {ElForm} from "element-plus";
import {listSkuPageByTradeId,listSkuPageByOrderId} from "@/api/basic/sku";
import {useRouter} from "vue-router";
import {useBasicStore} from '@/store/modules/basic'

const { proxy } = getCurrentInstance()
const tradeId = ref(null);
const orderId = ref(null);
const router = useRouter()
const loading = ref(false)
const deptOptions = ref([]);
const query = reactive({
  goodsName: '',
  goodsNo: '',
  skuName: '',
  barcode: ''
});
const selectItemSkuVoCheck = ref([])
const skuSelectFormRef = ref(null)
const total = ref(0);
const selectedSkuIds = ref(null)
const pageReq = reactive({
  page: 1,
  size: 10,
});
const list = ref([]);
const rightList = ref([]);
const rightListKeySet = computed(() => {
  const set = new Set();
  rightList.value.forEach((it) => set.add(it.id));
  return set
});

const loadAll = () => {
  pageReq.page = 1
  getPage()
};
const getRowKey = (row: any) => {
  return row.id;
}
const getPage = () => {
  const data = {
    ...query,
    pageNum: pageReq.page,
    pageSize: pageReq.size,
    tradeId: tradeId.value,
    orderId: orderId.value
  }
  loading.value = true
  if(tradeId.value!=null){
    listSkuPageByTradeId(data).then((res) => {
      const content = [...res.rows];
      list.value = content.map((it) => ({...it, id: it.skuId, checked: false}));
      total.value = res.total;
    }).then(() => toggleSelection()).finally(() => loading.value = false);
  }else{
    listSkuPageByOrderId(data).then((res) => {
      const content = [...res.rows];
      list.value = content.map((it) => ({...it, id: it.skuId, checked: false}));
      total.value = res.total;
    }).then(() => toggleSelection()).finally(() => loading.value = false);
  }
}
const getList = () => {
  getPage()
}
const getListBySalesTradeId = (id) => {
  tradeId.value = id
  getPage()
}
const getListByPurchaseOrderId = (id) => {
  orderId.value = id
  getPage()
}
const goCreateItem = () => {
  const data = proxy.$router.resolve({path: '/system/itemManage2', query: {openDrawer: true}})
  window.open(data.href, '_blank')
}
// 定义props
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  size: {
    type: String,
    default: '30%'
  },
  singleSelect:{
    type: Boolean,
    default: false
  },
  selectedSku: {
    type: Array,
    default: []
  }
})

const show = computed(() => {
  return props.modelValue;
})

// 定义事件
const emit = defineEmits<{
  (e: 'handleCancelClick')
  (e: 'handleOkClick', value)
  (e: 'handleSingleOkClick', value)
}>()

function handleCancelClick() {
  emit('handleCancelClick');
  clearQuantity()
}

function handleChooseSku(row) {
  emit('handleSingleOkClick', row);
}

function handleOkClick() {
  emit('handleOkClick', selectItemSkuVoCheck.value);
  clearQuantity()
}
/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  selectItemSkuVoCheck.value = selection
}

const toggleSelection = () => {
  props.selectedSku.forEach(selected => {
    const index = list.value.findIndex(it => selected.id=== it.id)
    if (index !== -1) {
      skuSelectFormRef.value.toggleRowSelection(list.value[index], true)
    }
  })
}

const judgeSelectable = (row) => {
  if (props.selectedSku.find(selected => selected.id === row.id)) {
    return false;
  }
  return true
}


function clearQuantity() {
  skuSelectFormRef.value.clearSelection()
}

const getVolumeText = (row) => {
  if((row.length || row.length === 0) && (row.width || row.width === 0) && (row.height || row.height === 0)) {
    return row.length + ' * ' + row.width + ' * ' + row.height
  }
  return ((row.length || row.length === 0) ? ('长：' + row.length) : '')
    + ((row.width || row.width === 0) ? (' 宽：' + row.width) : '')
    + ((row.height || row.height === 0) ? (' 高：' + row.height) : '')
}
defineExpose({
  getList,
  getListByPurchaseOrderId,
  getListBySalesTradeId
})
</script>
<style lang="scss">
.el-table .my-cell {
  vertical-align: top
}
</style>

